package com.logistics.modules.transportationManagement.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.logistics.modules.transportationManagement.dao.CargoDao;
import com.logistics.modules.transportationManagement.entity.ClientorderdetailsEntity;
import com.logistics.modules.warehouseManagement.dao.SeatDao;
import com.logistics.modules.warehouseManagement.entity.SeatEntity;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.common.utils.PageUtils;
import com.logistics.common.utils.Query;

import com.logistics.modules.transportationManagement.dao.ClientorderDao;
import com.logistics.modules.transportationManagement.entity.ClientorderEntity;
import com.logistics.modules.transportationManagement.service.ClientorderService;
import org.springframework.transaction.annotation.Transactional;


@Service("clientorderService")
public class ClientorderServiceImpl extends ServiceImpl<ClientorderDao, ClientorderEntity> implements ClientorderService {


    private ClientorderDao clientorderDao;

    private CargoDao cargoDao;

    private SeatDao seatDao;

    public ClientorderServiceImpl(ClientorderDao clientorderDao, CargoDao cargoDao, SeatDao seatDao) {
        this.clientorderDao = clientorderDao;
        this.cargoDao = cargoDao;
        this.seatDao = seatDao;
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 转换参数
        Integer cargoType = Convert.toInt(params.get("cargoType"));
        String cargoName = Convert.toStr(params.get("cargoName"));
        String lastName = Convert.toStr(params.get("lastName"));
        String phoneNumber = Convert.toStr(params.get("phoneNumber"));
        Boolean status = Convert.toBool(params.get("status"));
        // 实例化page对象
        IPage<ClientorderEntity> page = new Query<ClientorderEntity>().getPage(params);
        // 取数据库查询
        clientorderDao.selectAll(page,cargoType,cargoName,lastName,phoneNumber,status);

        return new PageUtils(page);
    }

    /**
     * 保存客户订单
     *
     * @param map 地图
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveClientOrder(Map<String, Object> map) {
        // 实例化实体类
        ClientorderEntity clientorderEntity = new ClientorderEntity();

        // 为实体类赋值
        BeanUtil.fillBeanWithMap(map, clientorderEntity, false);

        // 设置时间
        clientorderEntity.setCreateTime(new Date());

        // 保存客户订单
        int insert = clientorderDao.insert(clientorderEntity);

        if (insert <= 0) {
            throw new RuntimeException("客户订单添加失败! 请联系管理员!");
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateClientOrder(Map<String, Object> map) {
        // 实例化实体类
        ClientorderEntity clientorderEntity = new ClientorderEntity();

        // 为实体类赋值
        BeanUtil.fillBeanWithMap(map, clientorderEntity, false);

        // 设置时间
        clientorderEntity.setCreateTime(new Date());

        // 修改客户订单
        int insert = clientorderDao.updateSelective(clientorderEntity);

        // 判断
        if (insert <= 0) {
            throw new RuntimeException("客户订单更新失败! 请联系管理员!");
        }
    }

    @Override
    public ClientorderEntity selectOneById(Integer id) {
        return clientorderDao.selectOneById(id);
    }

    /**
     * 完成入库操作并返回客户订单
     *
     * @param clientOrderId 客户订单id
     * @param storehouse
     * @return {@link ClientorderEntity}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ClientorderEntity inventoryAndReturnCustomerOrderInformation(Integer clientOrderId, Integer storehouse) {
        // 1. 通过id拿到客户订单
        ClientorderEntity clientorderEntity = clientorderDao.selectOneById(clientOrderId);
        // 2. 根据货物里的货物类型找到席位保存到里面 - 货物全部保存到席位中
        clientorderEntity = findTheSeatAndSaveItAccordingToTheTypeOfCargoInTheCargo
                (clientorderEntity.getClientorderdetails(),clientorderEntity,storehouse);
        // 3. 修改客户订单 订单状态为已处理
        clientorderEntity.setStatus(1);
        // 4. 设置完成时间
        clientorderEntity.setFinishTime(new Date());
        // 5. 保存到仓库
        clientorderDao.updateSelective(clientorderEntity);
        // 6. 返回客户订单对象
        return clientorderEntity;
    }

    @Override
    public Map<String,Object> SelectAllClientorder() {
        //已完成订单
        List<Integer> clientorderEntities = clientorderDao.selectAllClientorderCompleted();
        //今年已完成订单
        List<Integer> Year = clientorderDao.selectAllClientorderCompletedYear();
        // names
        List<String> names = clientorderDao.getTheDateOfTheLastSevenDays();
        Map<String,Object> map = new HashMap<>();
        //处理结果  返回页面
        map.put("yiwancheng",clientorderEntities);
        map.put("year",Year);
        map.put("names", names);

        return map;
    }

    /**
     * 找到座位,保存它根据货物货物类型
     *
     * @param clientorderdetailsEntities            货物
     * @param clientorderEntity clientorder实体
     * @param storehouse
     * @return {@link ClientorderEntity}
     */
    private ClientorderEntity findTheSeatAndSaveItAccordingToTheTypeOfCargoInTheCargo(List<ClientorderdetailsEntity> clientorderdetailsEntities, ClientorderEntity clientorderEntity, Integer storehouse) {
        // 遍历集合
        for (ClientorderdetailsEntity cod : clientorderdetailsEntities) {
            // 根据这个货物类型id取数据库中匹配到一个席位
            SeatEntity seat = seatDao.selectOneByCargoTypeIdAndRepositoryId(cod.getCargo().getCargoTypeId(), storehouse);
            // 设置id
            seat.setCargoId(cod.getCargo().getId());
            // 设置保存的时间
            seat.setCargoSaveSeat(new Date());
            // 设置仓席位的状态
            seat.setSeatStatus(1);
            // 修改席位的状态
            seatDao.updateSelective(seat);
            // 修改客户订单详情的席位
            cod.setSeatId(seat.getId());
        }
        // 设置修改后的值
        clientorderEntity.setClientorderdetails(clientorderdetailsEntities);
        // 返回
        return clientorderEntity;
    }
}